package Stack;

import linkedlist.LinkedList;

/**
 * @author zhaoziqiang
 * @Description: 链表栈
 * @date 2020/8/28 17:21
 */
public class LinkedListStack<E> implements Stack<E>{
    private LinkedList<E> linkedList;
    public LinkedListStack(){
        linkedList=new LinkedList();
    }
    @Override
    public int getSize() {
        return linkedList.getSize();
    }

    @Override
    public int getCapacity() {
        return linkedList.getSize();
    }

    @Override
    public boolean isEmpty() {
        return linkedList.getSize()==0;
    }

    @Override
    public void push(E e) {
        linkedList.addHead(e);
    }

    @Override
    public E pop() {
        return linkedList.removeHead();
    }

    @Override
    public E peek() {
        return linkedList.getHead();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("LinkedListStack:top ");
        sb.append(linkedList);
        return sb.toString();
    }


    public static void main(String[] args) {

        LinkedListStack<Integer> as = new LinkedListStack<Integer>();
        for (int i = 0; i < 5; i++) {
            as.push(i);
        }
        System.out.println(as);
        as.pop();
        System.out.println(as);

    }
}
